package miiteen.customsportal.news.service;

import miiteen.customsportal.film.beans.FilmResource;
import miiteen.customsportal.film.dao.FilmResourceDao;
import miiteen.customsportal.html.beans.HtmlContent;
import miiteen.customsportal.html.dao.HtmlContentDao;
import miiteen.hr.pubconfig.CurrentHrInfo;
import miiteen.hr.user.beans.UserUserInfo;
import miiteen.sys.login.bean.UserShiro;
import miiteen.sys.login.utils.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import miiteen.data.dao.BaseDao;
import miiteen.data.service.impl.BaseServiceImpl;

import miiteen.customsportal.news.beans.NewsInfo;
import miiteen.customsportal.news.dao.NewsInfoDao;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *Name: CustomsPortal_News_Info
 *Des: -Service
 *Author: lx
 *Date: 2020-07-10
 */
@Service("newsInfoService")
public class NewsInfoService extends BaseServiceImpl<NewsInfo> {

	@Autowired
	private NewsInfoDao newsInfoDao;

	@Autowired
	private FilmResourceDao filmResourceDao;

	@Autowired
	private HtmlContentDao htmlContentDao;

	@Override
	protected BaseDao<NewsInfo> getDao() {
		return newsInfoDao;
	}

	@Transactional
	public List<NewsInfo> findListByMapPreviousNext(Map<String, Object> map){
		return newsInfoDao.findListByMapPreviousNext(map);
	}


	@Transactional
	public long save(NewsInfo newsInfo){

		UserShiro.ShiroUser shiroUser = LoginUtil.getCurUser();
		UserUserInfo userInfo = CurrentHrInfo.getUserInfo(shiroUser);
		boolean isUpdate=newsInfo.getId()>0;
		if(newsInfo.getId()>0){
			newsInfo.setUpdateId(userInfo.getAccountId());
			newsInfo.setUpdator(userInfo.getTrueName());
			newsInfo.setUpdateTime(new Date());
		}else{
			newsInfo.setCreateId(userInfo.getAccountId());
			newsInfo.setCreator(userInfo.getTrueName());
			newsInfo.setCreateTime(new Date());
		}


		if(newsInfo.getImgSrc()!=null&&newsInfo.getImgSrc().length>0){
			//判断当前是否是修改操作
			//现在正文的图片用富文本代替了，这里原先上传正文图片的地方，替换为存到预览图片picture里面
			newsInfo.setPicture(newsInfo.getImgSrc()[0]);
			/*if(newsInfo.getId()>0){
				Map<String,Object> map=new HashMap<>();
				map.put("newsInfoId",newsInfo.getId());
				String id="'"+newsInfo.getId()+"'";
				filmResourceDao.deleteNewsInfoId(id);
			}
			for(int i=0;i<newsInfo.getImgSrc().length;i++){
				FilmResource filmResource = new FilmResource();
				filmResource.setNewsInfoId(result);
				filmResource.setId(0);
				filmResource.setImage(newsInfo.getImgSrc()[i]);
				filmResourceDao.save(filmResource);
			}*/
		}
		long result = newsInfoDao.save(newsInfo);
		if(result>0){
			HtmlContent htmlContent=new HtmlContent();
			htmlContent.setInfoId(newsInfo.getId());
			htmlContent.setHtmlContent(newsInfo.getHtmlContent());
			htmlContent.setType(2);
			Map<String,Object> map=new HashMap<>();
			map.put("infoId",newsInfo.getId());
			List<HtmlContent> list= htmlContentDao.findListByMap(map);
			if(isUpdate){
				if(list.size()>0){
					htmlContent.setId(list.get(0).getId());
				}
			}
			htmlContentDao.save(htmlContent);

		}
		return result;
	}

	@Transactional
	public  List<NewsInfo>  getpage(){
		 return newsInfoDao.getpage();
	}

}